﻿using System.Text.RegularExpressions;
using express.bttree;

namespace express.compiler;

public class MethodInfo
{
    public String MethodName { get; set; }
    public List<object> Params { get; set; }
    public Operator Op { get; set; }
    public Object Result { get; set; }
    
    
    private static Object ConvertValue(Object param)
    {
        String sParam = param.ToString();
        Match matcher = Regexs.STRING_REG.Match(sParam);
        if(matcher.Success) {
            return matcher.Groups[2].Value;
        } else
        {
            var isBoolean = string.Equals(sParam.ToLower(),"true") || string.Equals(sParam.ToLower(), "false");
            if(isBoolean) {
                return Convert.ToBoolean(sParam);
            } else {
                if(StringUtils.isNumeric(sParam)) {
                    if(StringUtils.isDigits(sParam)) {
                        long longVal = Convert.ToInt64(sParam);
                        if(longVal > Int32.MaxValue) {
                            return longVal;
                        } else {
                            return (int) longVal;
                        }
                    } else {
                        double doubleVal = Convert.ToDouble(sParam);
                        if(doubleVal > float.MaxValue) {
                            return doubleVal;
                        } else {
                            return (float)doubleVal;
                        }
                    }
                } else {
                    return sParam;
                }
            }
        }
    }

    public static MethodInfo convert(List<SyntaxNode> syntaxNodes) {
        if(syntaxNodes.Count() == 0) {
            throw new Exception("列表不能为空");
        }
        String methodName = syntaxNodes[0].Value;
        List<Object> paramters = new List<Object>();
        Operator _operator = Operator.nil;
        List<Object> results = new List<object>();
        for (int i = 1; i < syntaxNodes.Count(); i++) {
            SyntaxNode syntaxNode = syntaxNodes[i];
            if(_operator == Operator.nil) {
                Operator op = Utils.GetOperator(syntaxNode.Value);
                if(op != Operator.nil) {
                    _operator = op;
                } else {
                    paramters.Add(ConvertValue(syntaxNode.Value));
                }
                continue;
            }
            results.Add(syntaxNode.Value);
        }

        MethodInfo methodInfo = new MethodInfo();
        methodInfo.MethodName = methodName;
        methodInfo.Params = paramters;
        if(_operator != Operator.nil) {
            methodInfo.Op  = _operator;
            if(results.Count() != 1) {
                throw new Exception("结果错误，" + results);
            } else {
                methodInfo.Result = ConvertValue(results[0]);
            }
        } else {
            if(results.Count > 0) {
                throw new Exception("结果错误，" + results);
            } else {
                methodInfo.Op = Operator.eq;
                methodInfo.Result = true;
            }
        }
        return methodInfo;

    }
}